package xyz.xiaomuyu.moment_read_app.Fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;

import xyz.xiaomuyu.moment_read_app.Adapter.HomePagerAdapter;
import xyz.xiaomuyu.moment_read_app.R;

public class HomeFragment extends Fragment {

    private TabLayout mTabLayout;
    private ViewPager2 mViewPager2;
    private List<String> mData = new ArrayList<>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_home, container, false);

        initData();

        initView(rootView);

        return rootView;
    }

    private void initView(View rootView) {


        mTabLayout = rootView.findViewById(R.id.home_indicator);

       mViewPager2 = rootView.findViewById(R.id.home_pager);

        HomePagerAdapter homePagerAdapter = new HomePagerAdapter(getActivity(), mData);
        mViewPager2.setAdapter(homePagerAdapter);

         mViewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback(){
        public void onPageSelected(int position) {
            switch (position){
                case 0:

            }
            super.onPageSelected(position);
            Log.d("go", "ViewPager onPageSelected " + position);
        }
    });
        new TabLayoutMediator(mTabLayout, mViewPager2, new TabLayoutMediator.TabConfigurationStrategy() {

            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {

                tab.setText(mData.get(position));

            }

        }).attach();
    }

    private void initData() {

        mData.add("阅读");
        mData.add("图文");
        mData.add("音乐");

    }




}

